//
//  UserRegiViewController.m
//  HuateVideo
//
//  Created by  on 12-5-29.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UserRegiViewController.h"
#import "UserCell.h"
#import "UserLoginResponse.h"
#import "AddUserResponse.h"
@interface UserRegiViewController ()

@end

@implementation UserRegiViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.view.backgroundColor=TOOLBAR_TINTCOLOR; 
    self.title=LS(@"注册");
    UIBarButtonItem* close=[[UIBarButtonItem alloc] initWithTitle:LS(@"关闭") style:UIBarButtonItemStylePlain target:self action:@selector(closeModal)];
    self.navigationItem.rightBarButtonItem=close;
    self.navigationController.navigationBar.tintColor = TOOLBAR_TINTCOLOR;
    [close release];
    [self initTableFooter];
}

-(void)closeModal{
    [self.navigationController dismissModalViewControllerAnimated:YES];
}

-(void)initTableFooter{
    UITableView* table=[[UITableView alloc] initWithFrame:CGRectMake(10, 30, 300, 300) style:UITableViewStyleGrouped];
    table.delegate=self;
    table.tag=11111;
    table.dataSource=self;
    table.opaque=NO;
    table.allowsSelection=NO;
    table.backgroundColor=[UIColor clearColor];
    table.backgroundView=nil;
    table.scrollEnabled=NO;
    table.separatorStyle=UITableViewCellSelectionStyleNone;
    [self.view addSubview:table];
    [table release];
    
    UIView*footer=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
    footer.backgroundColor=[UIColor clearColor];
    table.tableFooterView=footer;
    [footer release];
    
    
    UIButton* reg=[UIButton buttonWithType:UIButtonTypeCustom];
    reg.frame=CGRectMake(122, 10, 65, 30);
    [reg setBackgroundImage:[UIImage imageNamed:@"sizianniu.png"] forState:UIControlStateNormal];
    [reg setTitle:LS(@"注册") forState:UIControlStateNormal];
    [reg addTarget:self action:@selector(registerPressed) forControlEvents:UIControlEventTouchUpInside];
    [footer addSubview:reg];
}

-(void)registerPressed{
    
    UITableView* t=(UITableView*)[self.view viewWithTag:11111];
    UserCell*cell=(UserCell*)[t cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    UserCell*cell1=(UserCell*)[t cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    UserCell*cell2=(UserCell*)[t cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
    UserCell*cell3=(UserCell*)[t cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
    if (cell.cellTextFiled.text.length&&cell1.cellTextFiled.text.length&&cell2.cellTextFiled.text.length&&cell3.cellTextFiled.text.length) {
        if (![cell2.cellTextFiled.text isEqualToString: cell1.cellTextFiled.text ]) {
            UIAlertView* al=[[UIAlertView alloc] initWithTitle:LS(@"提示") message:LS(@"两次密码输入不同") delegate:nil cancelButtonTitle:LS(@"确定") otherButtonTitles: nil];
            [al show];
            [al release];
            return;
        }
        
    }else {        
        UIAlertView* al=[[UIAlertView alloc] initWithTitle:LS(@"提示") message:LS(@"请输入用户名及密码") delegate:nil cancelButtonTitle:LS(@"确定") otherButtonTitles: nil];
        [al show];
        [al release];
        return;
    }
    CommonWebServices* webServ=[[CommonWebServices alloc] init];
    webServ.delegate=self;
    NSMutableDictionary *parma = [NSMutableDictionary dictionary];
    [parma setValue:cell.cellTextFiled.text forKey:@"UserName"];

    [parma setValue:cell3.cellTextFiled.text forKey:@"email"];
    [parma setValue:cell1.cellTextFiled.text forKey:@"Password"];
    [webServ startRequestWithMode:@"UserInfo" Action:@"AddUser" Parma:parma Class:@"AddUserResponse" RootXML:@"AddUserResponse" resultIsArray:NO];
    NSLog(@"%@",parma);
    [webServ release];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString*identy=@"cell";
    UserCell*cell=(UserCell*)[tableView dequeueReusableCellWithIdentifier:identy];
    if (cell==nil) {
        cell=[[UserCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identy];
        cell.cellTextFiled.delegate=self;
    }
    if (indexPath.row==0) {
        cell.cellLab.text=[NSString stringWithFormat:@"%@:",LS(@"用户名")];
    }else if(indexPath.row==1) {
        cell.cellLab.text=[NSString stringWithFormat:@"%@:",LS(@"密码")];
        cell.cellTextFiled.secureTextEntry=YES;
    }else if(indexPath.row==2) {
        cell.cellLab.text=[NSString stringWithFormat:@"%@:",LS(@"确认密码")];
        cell.cellTextFiled.secureTextEntry=YES;
    }else {
        cell.cellLab.text=[NSString stringWithFormat:@"%@:",LS(@"注册邮箱")];
        cell.cellTextFiled.returnKeyType=UIReturnKeyDone;
    }
    
    return  cell;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    UITableView* t=(UITableView*)[self.view viewWithTag:11111];
    UserCell*cell1=(UserCell*)[t cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    UserCell*cell2=(UserCell*)[t cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
    UserCell*cell3=(UserCell*)[t cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];

    if (textField==cell1.cellTextFiled) {
        t.frame=CGRectMake(10, -30, 300, 350);
    }else if(textField==cell2.cellTextFiled){
        t.frame=CGRectMake(10, -50, 300, 350);
    }else if(textField==cell3.cellTextFiled){
        t.frame=CGRectMake(10, -80, 300, 350);
    }else {
        t.frame=CGRectMake(10, 30, 300, 350);
    }    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    UITableView* t=(UITableView*)[self.view viewWithTag:11111];
    UserCell*cell=(UserCell*)[t cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    UserCell*cell1=(UserCell*)[t cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    UserCell*cell2=(UserCell*)[t cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
    UserCell*cell3=(UserCell*)[t cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];

    if (textField==cell.cellTextFiled) {
        [cell1.cellTextFiled becomeFirstResponder];
    }else if(textField==cell1.cellTextFiled){
        [cell2.cellTextFiled becomeFirstResponder];
    }else if(textField==cell2.cellTextFiled){
        [cell3.cellTextFiled becomeFirstResponder];
    }else {
        t.frame=CGRectMake(10, 30, 300, 350);
        [textField resignFirstResponder];
    }
    return YES;
}
#pragma mark - CommonWebServicesDelegate

- (void)onCommonWebServicesWithCustomClass:(id)object {
    if ([object respondsToSelector:@selector(setAddUserResult:)]) {
        AddUserResponse*response=(AddUserResponse*)object;
        if (response.AddUserResult == -2) {
            NSLog(@"%d 表示存在重复的用户名或登陆名",response.AddUserResult);
            [self.view showAlert:LS(@"提示") message:LS(@"用户名已存在")];
        }        
        else if (response.AddUserResult == -1) {
            NSLog(@"%d 表示无权限访问",response.AddUserResult);
            [self.view showAlert:LS(@"提示") message:LS(@"没有权限访问")];

        }
        else if (response.AddUserResult == -100) {
            NSLog(@"%d 表示传入参数非法",response.AddUserResult);
            [self.view showAlert:LS(@"提示") message:LS(@"传入参数非法")];
        }else {
//            注册成功，modal去掉
            NSLog(@"%d 新增用户ID号",response.AddUserResult);
            
            UITableView* t=(UITableView*)[self.view viewWithTag:11111];
            UserCell*cell=(UserCell*)[t cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
            UserCell*cell1=(UserCell*)[t cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
                
                CommonWebServices *webServ = [[CommonWebServices alloc] init];
                webServ.delegate = self;
                NSMutableDictionary *parma = [NSMutableDictionary dictionary];
                [parma setValue:cell.cellTextFiled.text forKey:@"UserName"];
                [parma setValue:cell1.cellTextFiled.text forKey:@"PassWord"];
                [webServ startRequestWithMode:@"UserInfo" Action:@"UserLogin" Parma:parma Class:@"UserLoginResponse" RootXML:@"UserLoginResponse" resultIsArray:NO];
                [webServ release];
        }
    }else {
        UserLoginResponse *userLoginResponse = (UserLoginResponse *)object;
        if (userLoginResponse.UserLoginResult == 0) {
            NSLog(@"%d 表示登陆不成功",userLoginResponse.UserLoginResult);
            [self.view showAlert:LS(@"提示") message:LS(@"登陆失败")];
        }
        else if (userLoginResponse.UserLoginResult >= 1) {
            NSLog(@"%d 表示登陆成功,返回值为用户ID",userLoginResponse.UserLoginResult);
            UITableView* t=(UITableView*)[self.view viewWithTag:11111];
            UserCell*cell=(UserCell*)[t cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
            [UserDefaults setObject:cell.cellTextFiled.text forKey:USERNAME_STORE_KEY];

            UserCell*cell1=(UserCell*)[t cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
            [UserDefaults setObject:cell1.cellTextFiled.text forKey:PASSWORD_STORE_KEY];
            [[NSNotificationCenter defaultCenter] postNotificationName:USERLOGIN_ACTION object:cell.cellTextFiled.text];
            [self.navigationController dismissModalViewControllerAnimated:YES];
        }
        else if (userLoginResponse.UserLoginResult == -1) {
            NSLog(@"%d 表示没有权限操作",userLoginResponse.UserLoginResult);
            [self.view showAlert:LS(@"提示") message:LS(@"没有权限操作")];
        }
        else if (userLoginResponse.UserLoginResult == -100) {
            NSLog(@"%d 表示传入参数非法",userLoginResponse.UserLoginResult);
            [self.view showAlert:LS(@"提示") message:LS(@"传入参数非法")];
        }
    }
    
}

- (void)onCommonWebServicesFaild
{
    NSLog(@"失败");
    [self.view showAlert:LS(@"提示") message:LS(@"服务器连接失败")];
}

- (void)openActiovityIndicator {
    //[self.view showProgressText:YES];
}
- (void)closeActiovityIndicator {
    //[self.view showProgressText:NO];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
